package com.bawei.module_live.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_live.R
import com.bawei.module_live.version1.GoodsEntity
import com.bumptech.glide.Glide

class GoodsAdapter(
    private val list: List<GoodsEntity>,
    private val onGoodsClick: (GoodsEntity) -> Unit
) : RecyclerView.Adapter<GoodsAdapter.GoodsViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GoodsViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_goods, parent, false)
        return GoodsViewHolder(view)
    }

    override fun onBindViewHolder(holder: GoodsViewHolder, position: Int) {
        val goods = list[position]
        holder.tvName.text = goods.goods_desc
        holder.tvPrice.text = "￥${goods.goods_default_price}"
        Glide.with(holder.ivIcon).load(goods.goods_default_icon).into(holder.ivIcon)
        holder.itemView.setOnClickListener { onGoodsClick(goods) }
    }

    override fun getItemCount(): Int = list.size

    class GoodsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val ivIcon: ImageView = itemView.findViewById(R.id.iv_goods_icon)
        val tvName: TextView = itemView.findViewById(R.id.tv_goods_name)
        val tvPrice: TextView = itemView.findViewById(R.id.tv_goods_price)
    }
} 